package com.photographysharing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photographysharing.common.ThreadLocalOperation;
import com.photographysharing.dto.LikeDTO;
import com.photographysharing.entity.Likes;
import com.photographysharing.entity.Statistics;
import com.photographysharing.exception.ServiceException;
import com.photographysharing.request.SelectByPaging;
import com.photographysharing.response.DatasResponse;
import com.photographysharing.response.MessageResponse;
import com.photographysharing.response.SingleResponse;
import com.photographysharing.service.LikesService;
import com.photographysharing.service.StarsService;
import com.photographysharing.service.StatisticsService;
import com.photographysharing.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/like")
@RestController
public class LikeController {

    private ThreadLocal<LikeController> threadLocal;


    private LikesService likesService;
    private StatisticsService statisticsService;

    public LikeController(LikesService likesService,StatisticsService statisticsService) {

        this.likesService = likesService;
        this.statisticsService = statisticsService;
    }

    @GetMapping("/like")
    public MessageResponse like(String photoId){
        try {
            String userId = ThreadLocalOperation.getThreadLocalUserId();
            Likes like = new Likes(userId,photoId);
            QueryWrapper<Likes> qw = new QueryWrapper<>();
            qw.eq("user_id",userId);
            qw.eq("photo_id",photoId);
            if(isLike(photoId)){
                if(likesService.remove(qw)){
                    statisticsService.reduceLike(photoId);
                    return new MessageResponse("取消成功!");
                }
            }
            if(likesService.save(like)){
                //调用统计服务增加点赞数
                statisticsService.increaseLike(photoId);
                return new MessageResponse("点赞成功!");
            }
            return new MessageResponse("点赞失败!",false);
        }catch (Exception e ){
            throw new ServiceException(e.getMessage());
        }
    }
    @GetMapping("/isLike")
    public boolean isLike(String photoId){
        //获取用户Id
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        Likes like = new Likes(userId,photoId);
        QueryWrapper<Likes> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("photo_id",photoId);
        //判断当前是否存在点赞
        return likesService.getOne(qw) != null?true:false;
    }

    @GetMapping("/likeByMy")
    public DatasResponse<LikeDTO> likeByMy(SelectByPaging pag){
        String userId = ThreadLocalOperation.getThreadLocalUserId();
        pag.setSearchValue1(userId);
       return likesService.select(pag);
    }
}
